home *** CD-ROM | disk | FTP | other *** search
/ PC User 2002 April / Disc 1 / PCUSER0402D1.iso / resource / ecomm / files / ecomm205.exe / data1.cab / Projects / Demo / Web / ecomm.js < prev    next >
Encoding:
Text File  |  2001-11-26  |  10.5 KB  |  357 lines

  1. <!--
  2. // -----
  3. // item
  4. // -----
  5. function item(url, title, price, image, weight, freight, tax)
  6. {
  7.  
  8.   this.count = 0;
  9.  
  10.   this.url = url.substring(0);
  11.   this.title = title.substring(0);
  12.   this.price = (price) ? price : 0;
  13.   this.image = image.substring(0);
  14.   this.weight = (weight) ? weight : 0;
  15.   this.freight = (freight) ? freight : 0;
  16.   this.tax = (tax) ? tax : 0;
  17.  
  18. }
  19.  
  20. function addItem()
  21. {
  22.   var url;
  23.   var code;
  24.   var title;
  25.   var price;
  26.   var image;
  27.   var qty;
  28.   var weight;
  29.   var freight;
  30.   var tax;
  31.  
  32.   for (var i = 0; i < arguments.length; i++)
  33.   {
  34.     if (i == 0) url = arguments[i];
  35.     if (i == 1) code = arguments[i];
  36.     if (i == 2) title = arguments[i];
  37.     if (i == 3) price = arguments[i];
  38.     if (i == 4) image = arguments[i];
  39.     if (i == 5) qty = arguments[i];
  40.     if (i == 6) weight = arguments[i];
  41.     if (i == 7) freight = arguments[i];
  42.     if (i == 8) tax = arguments[i];
  43.   }  
  44.  
  45.   price=fmt_num(price);
  46.   if (list[code] == null)
  47.     list[code] = new item(url, title, price, image, weight, freight ,tax);
  48.  
  49.   list[code].count += qty;
  50.  
  51.   if (parent.shoptrolley != null)
  52.     parent.shopregister.showBasket();
  53. }
  54.  
  55. function removeItem(code)
  56. {
  57.   if (list[code] != null && list[code].count > 0) {
  58.     list[code].count--;
  59.     parent.shopregister.showBasket();
  60.  
  61.   }
  62. }
  63.  
  64. function changeQty(key)
  65. {
  66.   var qty = list[key].count;
  67.  
  68.   if ((qty = prompt("Enter new quantity", qty)) != null)
  69.     if (isNaN(qty = parseInt(qty)))
  70.       alert("Invalid number.");
  71.     else if (qty < 0)
  72.       alert("Quantity must be zero or greater.");
  73.     else {
  74.       list[key].count = qty;
  75.       showBasket();
  76.     }
  77. }
  78.  
  79.  
  80. function formatDescription(str)
  81. {
  82.   var newStr = unescape(str);
  83.  
  84.   if (newStr.length > descrLength)
  85.     newStr = newStr.substring(0, descrLength - 3) + "...";
  86.  
  87.   newStr += "</A>";
  88.  
  89.   while (newStr.length < descrLength + 4)
  90.     newStr += " ";
  91.  
  92.   return newStr;
  93. }
  94.  
  95. function formatPrice(price, width)
  96. {
  97.   var newPrice = "" + price;
  98.  
  99.   if (newPrice.lastIndexOf('.') == -1)
  100.     newPrice += '.';
  101.   else
  102.     newPrice = newPrice.substring(0, newPrice.lastIndexOf('.') + 3);
  103.  
  104.   while ((c = newPrice.lastIndexOf('.')) > newPrice.length - 3 && c > -1)
  105.     newPrice += '0';
  106.  
  107.   while (newPrice.length < width)
  108.     newPrice = ' ' + newPrice;
  109.  
  110.   return newPrice;
  111. }
  112.  
  113.  
  114. function sche(ALINK_COLOR, BGCOLOR, FRAME_COLOR, HEADING2, VLINK_COLOR, LIST_COLOR, TEXT_COLOR, LINK_COLOR,S_CELLCOLOR,S_CELLBORDER,S_BORDER,S_BORDER2,S_HEADING,S_BACKGROUND,CURRENCY)
  115. {
  116.   this.ALINK_COLOR = ALINK_COLOR;
  117.   this.BGCOLOR = BGCOLOR;
  118.   this.FRAME_COLOR = FRAME_COLOR;
  119.   this.HEADING2 = HEADING2;
  120.   this.VLINK_COLOR = VLINK_COLOR;
  121.   this.LIST_COLOR = LIST_COLOR;
  122.   this.TEXT_COLOR = TEXT_COLOR;
  123.   this.LINK_COLOR = LINK_COLOR;
  124.   this.S_CELLCOLOR = S_CELLCOLOR;
  125.   this.S_CELLBORDER= S_CELLBORDER;
  126.   this.S_BORDER= S_BORDER;
  127.   this.S_BORDER2= S_BORDER2;
  128.   this.S_HEADINGCOLOR= S_HEADING;
  129.   this.S_BACKGROUND= S_BACKGROUND;
  130.   this.CURRENCY=CURRENCY;
  131. }
  132.  
  133.  
  134. function showBasket()
  135. {
  136. // need for history 
  137. // register.html -> showBasket() -> trolley.html -> displayBasket()
  138. //
  139.   if (parent.shoptrolley != null)
  140.      if (parent.shoptrolley.location != null)
  141.          parent.shoptrolley.location = "trolley.html"; 
  142. }
  143.  
  144.  
  145. function displayBasket(){
  146.   var tablesize = 10;
  147.  
  148. // just only substitution
  149.   t_sche = parent.shopregister.t_she;
  150.  
  151.   parent.shoptrolley.document.open();
  152.   parent.shoptrolley.document.writeln('<HTML><HEAD><TITLE>Trolley</TITLE>');
  153.   parent.shoptrolley.document.writeln('<BASE TARGET="shopmain">');
  154.   parent.shoptrolley.document.writeln('</HEAD>');
  155.  
  156.  
  157.   parent.shoptrolley.document.writeln('<BODY TEXT="' + t_sche.TEXT_COLOR + 
  158.                                        '" BGCOLOR="' + t_sche.BGCOLOR + 
  159.                                        '" LINK="' + t_sche.LINK_COLOR + 
  160.                                        '" VLINK="' + t_sche.VLINK_COLOR + 
  161.                                        '" ALINK="' + t_sche.ALINK_COLOR +
  162.                                        '" BACKGROUND="' + t_sche.S_BACKGROUND
  163.                                        +'">'
  164.                                        );
  165.  
  166.  
  167.   parent.shoptrolley.document.write('<TABLE BORDER='+t_sche.S_BORDER+' CELLPADDING=0 CELLSPACING=0><TR>');
  168.   if (t_sche.HEADING2)
  169.   parent.shoptrolley.document.write('<TD>'+t_sche.HEADING2+'</td></tr><tr>');
  170.   
  171.   parent.shoptrolley.document.write('<TD BGCOLOR="'+t_sche.FRAME_COLOR+'" WIDTH=178> </TD>');
  172.   parent.shoptrolley.document.write(' </TR><TR><TD>');
  173.  
  174.   parent.shoptrolley.document.write('<TABLE WIDTH=178 BORDER=0 CELLPADDING=1 CELLSPACING=0 BGCOLOR="'+t_sche.FRAME_COLOR+'"><TR><TD>');
  175.   parent.shoptrolley.document.write('<TABLE WIDTH=100% BORDER='+t_sche.S_BORDER2+' CELLPADDING=0 CELLSPACING=0 BGCOLOR="'+t_sche.LIST_COLOR+'"><TR><TD>');
  176.   parent.shoptrolley.document.write('<TABLE WIDTH=100% BORDER='+t_sche.S_CELLBORDER+' CELLPADDING=2 CELLSPACING=4>');
  177.   parent.shoptrolley.document.write('<TR BGCOLOR="'+t_sche.LIST_COLOR+'">');
  178.   parent.shoptrolley.document.write('<TH WIDTH=20><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'> QTY </FONT></TH>');
  179.   parent.shoptrolley.document.write('<TH WIDTH=80><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'>ITEM</FONT></TH>');
  180.   parent.shoptrolley.document.write('<TH WIDTH=30><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'> COST </FONT></TH></TR>');
  181.  
  182.  
  183.  
  184. //
  185. // parent.shopregister.document.forms.inputForm.PrintOrder
  186. //
  187.   var doc = parent.shopregister.document.forms;
  188.  
  189.   doc.inputForm.PrintOrder.value = "";
  190.   doc.inputForm.PROD.value = "13:";
  191.   doc.inputForm.OrderID.value = "";
  192.   var startIdx = location.href.indexOf("register");
  193.   var productLink = location.href.substring(0,startIdx); 
  194.  
  195.   for (i in list)
  196.     if (list[i].count > 0) {
  197.       parent.shoptrolley.document.write('<TR BGCOLOR="'+t_sche.S_CELLCOLOR+'">');
  198.       parent.shoptrolley.document.write('<TD ALIGN=CENTER"><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  199.       parent.shoptrolley.document.write('<A HREF="javascript:parent.shopregister.changeQty(\''+i+'\')">'+list[i].count+'</A></FONT></TD>');
  200.  
  201.       parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  202.       parent.shoptrolley.document.write('<A HREF="'+list[i].url+'">'+formatDescription(unescape(list[i].title))+'</FONT></TD>');
  203.  
  204.       parent.shoptrolley.document.write('<TD NOWRAP ALIGN=RIGHT><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  205.       parent.shoptrolley.document.write(' '+t_sche.CURRENCY + formatPrice(list[i].price * list[i].count, 5) + '</FONT></TD></TR>');
  206.  
  207.       doc.inputForm.OrderID.value += i + "x" + list[i].count + ",";
  208.       doc.inputForm.PrintOrder.value += list[i].count + " x " + unescape(list[i].title + "... "+t_sche.CURRENCY+ formatPrice(list[i].price, 0)) + "<BR>";
  209.       doc.inputForm.PROD.value += list[i].count+"    "+unescape(list[i].title+"    "+i+"    "+formatPrice(list[i].price, 0))+"    "+list[i].weight+"    "+productLink+list[i].url+ "    "+t_sche.CURRENCY+"    "+list[i].freight+"    "+list[i].tax+"    0    0    0    "+"<BR>"+"    " ;
  210.       tablesize--;
  211.     }
  212.  
  213.   while (tablesize-- > 0){
  214.     parent.shoptrolley.document.write('<TR BGCOLOR='+t_sche.S_CELLCOLOR+'><TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT></TD>');
  215.     parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT></TD>');
  216.     parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT>');
  217.   }
  218.  
  219.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  220.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  221.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  222. //  parent.shoptrolley.document.write('</TD></TR></TABLE>');
  223.  
  224.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  225. //  parent.shoptrolley.document.write('</CENTER>');
  226.   parent.shoptrolley.document.write('</BODY></HTML>');
  227.   parent.shoptrolley.document.close();
  228.  
  229.  
  230.   doc.inputForm.Total.value = sumTotal();
  231.   doc.inputForm.Weight.value = sumWeight();
  232.   doc.inputForm.AddFreight.value = sumFreight();
  233.   doc.inputForm.TaxVal.value = sumTax();
  234. }
  235.  
  236. function sumTotal()
  237. {
  238.   var total = 0.0;
  239.   for (i in list) total += (list[i].price * list[i].count);
  240.   return formatPrice(total, 7);
  241. }
  242.  
  243. function sumWeight()
  244. {
  245.   var total = 0.0;
  246.   for (i in list) total += (list[i].weight * list[i].count);
  247.   if (total > 0 && total < 0.01)
  248.      total = 0.01;
  249.   return total;
  250. }
  251.  
  252. function sumFreight()
  253. {
  254.   var total = 0.0;
  255.   for (i in list) total += (list[i].freight * list[i].count);
  256.   return total;
  257. }
  258.  
  259. function sumTax()
  260. {
  261.   var total = 0.0;
  262.   for (i in list) total += (list[i].price * list[i].count * list[i].tax/100);
  263.   return total;
  264. }
  265.  
  266. function inTrolley()
  267. {
  268.    var q = 0;
  269.        
  270.    for (i in list){
  271.        if (list[i] != null && list[i].count > 0)
  272.           q += list[i].count;
  273.    } 
  274.    if (q > 0)
  275.       return true; 
  276.    else
  277.       return false; 
  278. }
  279.  
  280. //--
  281. // is str number?
  282. //--
  283.  function isNum(str) {
  284.    if(!str) return false;
  285.    for(var i=0; i<str.length; i++){
  286.      var ch=str.charAt(i);
  287.      if ("0123456789 .,".indexOf(ch) ==-1) return false;
  288.    }
  289.    return true;
  290.  } 
  291.  
  292. //--
  293. // change format of price:
  294. // 1,2.34,567.89  --> 1234567.89
  295. // 1,2.34,567,89  --> 1234567.89
  296. // 1,2.3 4,567,89 --> 1234567.89
  297. // 1,2.3a 4,567,89--> 0
  298. // 12.343         --> 12.343
  299. // 12,34          --> 12.34
  300. // 123            --> 123
  301.  function fmt_num(str) {
  302. var value=0.0;
  303. var x= str.lastIndexOf(".");
  304. var y= str.lastIndexOf(",");
  305.  
  306.  if ( !isNum(str) )
  307.    return(value);
  308.  
  309.  
  310.  if ( x == -1  && y == -1 ){
  311.    value = parseInt(str);
  312.    return(value);
  313.  }
  314.  
  315.  if ( x < y )
  316.     x=y;
  317.  
  318.  txt1=str.substring(0,x);
  319.  txt1=replace(txt1,',',''); 
  320.  txt1=replace(txt1,'.',''); 
  321.  txt1=replace(txt1,' ',''); 
  322.    
  323.  txt=txt1+'.'+str.substring(x+1);
  324.  value = (parseInt(txt1) + parseFloat('.'+str.substring(x+1)));
  325.  
  326. return(value);
  327. }
  328.  
  329. //-- replace 
  330. function replace(string,pattern,insert) {
  331.  var strLength = string.length, txtLength = pattern.length;
  332.  
  333.  if ((strLength == 0) || (txtLength == 0)) 
  334.     return string;
  335.  
  336.  var i = string.indexOf(pattern);
  337.  if ((!i) && (pattern != string.substring(0,txtLength))) 
  338.      return string;
  339.  if (i == -1) 
  340.     return string;
  341.  
  342.  var newstr = string.substring(0,i) + insert;
  343.  
  344.  if (i+txtLength < strLength)
  345.     newstr += replace(string.substring(i+txtLength,strLength),pattern,insert);
  346.  
  347. return newstr;
  348. }
  349.  
  350.  
  351. // --- MAIN ---
  352. var descrLength = 16;
  353. var t_she='1';
  354. list = new Array();
  355.  
  356. // -->
  357.